﻿using Common.Prism.Apps;
using NxChatQQ.Models;
using NxChatQQ.Models.Events;
using NxChatQQ.Views.Layouts;
using Prism.Commands;
using Prism.Events;
using Prism.Regions;
using System;
using System.Collections.ObjectModel;
using System.Windows.Input;

namespace NxChatQQ.ViewModels.Layouts
{
    public class FriendListLayoutViewModel : BaseViewModel
    {
        private string _InputText = "";
        public string InputText
        {
            get { return _InputText; }
            set { SetProperty(ref _InputText, value); }
        }

        private string _NameTitle = "";
        public string NameTitle
        {
            get { return _NameTitle; }
            set { SetProperty(ref _NameTitle, value); }
        }

        public ICommand SelectionChangedCommand { get; set; }
        public Action ScrollToBottomAction { get; set; }

        public ObservableCollection<TreeFriendModel> FriendItems { get; set; }

        private readonly IRegionManager _regionManager;
        public readonly IEventAggregator _eventAggregator;

        public FriendListLayoutViewModel(IEventAggregator eventAggregator, IRegionManager regionManager)
        {
            _regionManager = regionManager;
            _eventAggregator = eventAggregator;

            SelectionChangedCommand = new DelegateCommand<TreeFriendModel>(SelectionChanged);

            FriendItems = new ObservableCollection<TreeFriendModel>();

            _regionManager.RegisterViewWithRegion("ChatRegionContent", typeof(ChatMessageLayout));



            var items = new TreeFriendModel()
            {
                Name = "分类1",

                Items = new ObservableCollection<TreeFriendModel>() {
                    new TreeFriendModel(){
                        Friend = new Friend(){  Name = "逐梦1"}
                       },

                    new TreeFriendModel(){
                        Friend = new Friend(){  Name = "逐梦2"}
                       },

                    new TreeFriendModel(){
                        Friend = new Friend(){  Name = "逐梦3"}
                       },
                }
            };

            var items2 = new TreeFriendModel()
            {
                Name = "分类2",

                Items = new ObservableCollection<TreeFriendModel>() {
                    new TreeFriendModel(){
                        Friend = new Friend(){  Name = "逐梦42222222222222222222222222222222222222222222222222222222222222222222222"}
                       },

                    new TreeFriendModel(){
                        Friend = new Friend(){  Name = "逐梦5"}
                       },

                    new TreeFriendModel(){
                        Friend = new Friend(){  Name = "逐梦6"}
                       },
                    new TreeFriendModel(){
                        Friend = new Friend(){  Name = "逐梦799999999999999999999999999999999999999999999999"}
                       },
                    new TreeFriendModel(){
                        Friend = new Friend(){  Name = "逐梦8"}
                       },
                    new TreeFriendModel(){
                        Friend = new Friend(){  Name = "逐梦9"}
                       }, new TreeFriendModel(){
                        Friend = new Friend(){  Name = "逐梦10"}
                       }, new TreeFriendModel(){
                        Friend = new Friend(){  Name = "逐梦11"}
                       }, new TreeFriendModel(){
                        Friend = new Friend(){  Name = "逐梦12"}
                       }, new TreeFriendModel(){
                        Friend = new Friend(){  Name = "逐梦13"}
                       },
                }
            };

            FriendItems.Add(items);
            FriendItems.Add(items2);
        }

        private void SelectionChanged(TreeFriendModel node)
        {
            if (node == null)
            {
                _eventAggregator.GetEvent<UpdateFriendInfoEvent>().Publish(null);
            }
            else
            {
                if (!node.IsTitle)
                {
                    _eventAggregator.GetEvent<UpdateFriendInfoEvent>().Publish(node.Friend);
                }
            }
        }
    }
}
